import axios from "axios";
import { ElMessage } from "element-plus";

const request = axios.create({
    baseURL: import.meta.env.VITE_APP_BASE_API,
    timeout: 5000,
})

request.interceptors.request.use((config) => {
    if (localStorage.getItem('TOKEN')) {
        config.headers['Authorization'] = localStorage.getItem('TOKEN');
    }
    return config
})
request.interceptors.response.use((res) => {
    // console.log(res);
    if (res.config.responseType == "arraybuffer") {
        return res.data
    }
    if (res.data.code !== '200') {
        ElMessage({
            message: res.data.msg,
            type: 'error'
        })
        return Promise.reject('error')
    }

    return res.data


})

export default request